Namespace Aricie.DNN.Caching

    <Serializable()> _
    Public Structure CachePutJob

        Public Sub New(ByVal job As CacheJob, ByVal value As WrappedCacheObject)
            Me.Job = New CacheJob(job.StrategyType, job.Strategy, job.Key)
            Me.Value = value
        End Sub

        Public Sub New(ByVal key As String, ByVal job As CachePutJob)
            Me.New(New CacheJob(key, job.Job), job.Value)
        End Sub


        Public Job As CacheJob
        Public Value As WrappedCacheObject


        Public Function GetTimeOut() As TimeSpan
            Dim timeOut As TimeSpan = Cache.NoSlidingExpiration
            If Me.Value.SlidingExpriration <> Cache.NoSlidingExpiration Then
                timeOut = Me.Value.SlidingExpriration
            ElseIf Me.Value.AbsoluteExpiration <> Cache.NoAbsoluteExpiration Then
                If Me.Value.AbsoluteExpiration > Now Then
                    timeOut = Me.Value.AbsoluteExpiration.Subtract(Now)
                Else
                    timeOut = TimeSpan.Zero
                End If
            End If

            timeOut = Me.Job.GetTimeOut(timeOut)
        End Function


    End Structure

    <Serializable()> _
    Public Structure CacheJob

        Public StrategyType As StrategyType
        Public Strategy As CacheStrategy
        Public Key As String
        Private _FormattedKey As String
        'Public UnformattedKey As String
        'Public Value As WrappedCacheObject

        Public ReadOnly Property FormattedKey() As String
            Get
                If String.IsNullOrEmpty(_FormattedKey) Then
                    _FormattedKey = Me.Strategy.FormatKey(Me.Key)
                End If
                Return _FormattedKey
            End Get
        End Property

        Public ShouldLog As Boolean
        Public ShouldSynchronize As Boolean
        Public ShouldAddGlobalDep As Boolean

        Public Sub New(ByVal strategyType As StrategyType, _
                       ByVal strategy As CacheStrategy, _
                       ByVal key As String)

            Me.StrategyType = strategyType
            Me.Strategy = strategy
            'Me.UnformattedKey = key
            Me.Key = key 'strategy.FormatKey(key)
            Me.ShouldLog = DistributedCachingProviderBase.Settings.LoggingSettings.EnableStatsCollector AndAlso strategy.LogOperations
            Me.ShouldSynchronize = DistributedCachingProviderBase.Settings.CloudSettings.EnableSynchronization AndAlso Not strategy.DisableSynchronization
            Me.ShouldAddGlobalDep = DistributedCachingProviderBase.Settings.LocalCacheSettings.EnableGlobalDependency AndAlso strategy.AddGlobalDependency
        End Sub



        Public Sub New(ByVal strategyType As StrategyType, ByVal strategy As CacheStrategy)
            Me.New(strategyType, strategy, "")
        End Sub

        Public Sub New(ByVal key As String, ByVal job As CacheJob)
            Me.New(job.StrategyType, job.Strategy, key)
            Me._FormattedKey = job._FormattedKey
        End Sub

        Public Function GetTimeOut(ByVal initialTimeOut As TimeSpan) As TimeSpan
            Select Case Me.Strategy.TimeOutMode
                Case TimeOutMode.Unchanged
                    Return initialTimeOut
                Case TimeOutMode.Constant
                    Return TimeSpan.FromSeconds(Me.Strategy.TimeOutConstant)
                Case TimeOutMode.AdditiveConstant
                    Return initialTimeOut + TimeSpan.FromSeconds(Me.Strategy.TimeOutConstant)
                Case TimeOutMode.MultiplicativeConstant
                    Return TimeSpan.FromTicks(initialTimeOut.Ticks * Me.Strategy.TimeOutConstant)
                Case Else
                    Return Cache.NoSlidingExpiration
            End Select
        End Function


    End Structure
End NameSpace